﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace BenevolentSunData
{
    public class SinkCell : Cell
    {

        private String colorName;
        public String ColorName
        {
            get
            {
                return colorName;
            }
        }

        public override string Name
        {
            get
            {
                return "Exit";
            }
        }


        public override string Note
        {
            get
            {
                return "Hit with the correct color beam of light to exit! Requires " + colorName + " light.";
            }
        }


        #region Graphics
        [ContentSerializerIgnore]
        public Model Model;

        [ContentSerializerIgnore]
        public Material Material = new Material();
        #endregion


        #region State
        Color requiredColor;
        [ContentSerializer(Optional = true)]
        public Color RequiredColor
        {
            get { return requiredColor; }
            set { requiredColor = value; 
                Material.EmissiveColor = new Color(requiredColor.ToVector3() * 0.3f);
                if (requiredColor.Equals(Color.Red))
                    colorName = "Red";
                else if (requiredColor.Equals(Color.Lime))
                    colorName = "Green";
                else if (requiredColor.Equals(Color.Blue))
                    colorName = "Blue";
                else if (requiredColor.Equals(Color.Fuchsia))
                    colorName = "Pink";
                else if (requiredColor.Equals(Color.Yellow))
                    colorName = "Yellow";
                else if (requiredColor.Equals(Color.Aqua))
                    colorName = "Aqua";
                else if (requiredColor.Equals(Color.White))
                    colorName = "White";
                else
                    throw (new Exception("Unknown sink color: " + requiredColor));
            }
        }

        Direction entranceDirection = Direction.North;
        public Direction EntranceDirection
        {
            get { return entranceDirection; }
            set
            {
                entranceDirection = value;
                switch (entranceDirection)
                {
                    case Direction.North:
                        Rotation = (float)Math.PI;
                        break;
                    case Direction.South:
                        Rotation = 0;
                        break;
                    case Direction.East:
                        Rotation = (float)Math.PI * 0.5f;
                        break;
                    case Direction.West:
                        Rotation = (float)Math.PI * 1.5f;
                        break;
                }
            }
        }

        bool activated = false;
        public bool IsActivated
        {
            get { return activated; }
        }
        #endregion


        #region Graphics
        Matrix[] boneTransforms;
        #endregion


        #region Initialization
        public SinkCell()
        {
            EntranceDirection = Direction.North;
            RequiredColor = Color.White;
        }

        public override void LoadContent(ContentManager Content, Map map)
        {
            Material = map.Skin.GetMaterial("Sink");
            Model = Content.Load<Model>("Models/Sink");
            boneTransforms = new Matrix[Model.Bones.Count];
            Model.CopyAbsoluteBoneTransformsTo(boneTransforms);
            RequiredColor = requiredColor;
            base.LoadContent(Content, map);
        }
        #endregion


        #region Drawing
        public override void Draw(Vector3 position)
        {
            foreach (ModelMesh mesh in Model.Meshes)
            {
                Matrix world =
                    boneTransforms[mesh.ParentBone.Index] *
                    Matrix.CreateFromYawPitchRoll(Rotation, 0f, 0f) *
                    Matrix.CreateTranslation(position);
                Material.DrawMesh(mesh, world, Map.PointLights, Map.BeamLights);
            }
        }
        #endregion


        #region Beams
        public override void Clear()
        {
            activated = false;

            base.Clear();
        }

        public override List<Direction> AddInput(Beam beam, Direction direction)
        {
            MergeBeam(beam, direction);

            if (GetBeam(entranceDirection) != null && GetBeam(entranceDirection).Color.Equals(RequiredColor))
            {
                activated = true;

                //MergeBeam(GetBeam(entranceDirection), DirectionUtils.Invert(entranceDirection));
            }
            else
            {
                Clear();
                MergeBeam(beam, direction);
            }

            return base.AddInput(beam, direction);
        }
        #endregion


        #region ICloneable
        public override object Clone()
        {
            SinkCell cell = new SinkCell();
            cell.Coords = new Point(Coords.X, Coords.Y);
            cell.EntranceDirection = EntranceDirection;
            cell.RequiredColor = RequiredColor;
            cell.Model = Model;
            cell.Material = Material.Clone();
            cell.boneTransforms = boneTransforms;
            cell.floorBoneTransforms = floorBoneTransforms;
            cell.floorModel = floorModel;
            cell.floorMaterial = floorMaterial.Clone();
            return cell;
        }
        #endregion

    }
}
